package com.dst.smart.dao.impl.factory;

import java.util.ArrayList;
import java.util.List;

import com.dst.smart.app.MyApplication;
import com.dst.smart.model.RedInfra;

public class RedInfraFactory {
	public static List<RedInfra> list = new ArrayList<RedInfra>();

	static {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getInfrareds();
		}
	}

	public static List<RedInfra> getAll() {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getInfrareds();
		}
		return list;
	}

	public static List<RedInfra> getLimit(int limit) {

		List<RedInfra> resultList = new ArrayList<RedInfra>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getInfrareds();
		}

		if (limit < list.size()) {
			resultList = list.subList(0, limit);
		} else {
			resultList.addAll(list);
		}
		return resultList;
	}

	public static List<RedInfra> getInfraById(int id) {

		List<RedInfra> resultList = new ArrayList<RedInfra>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getInfrareds();
		}
		for (RedInfra infrared : list) {
			if (infrared.getFatherId() == id) {
				resultList.add(infrared);
			}
		}

		return resultList;
	}

	public static RedInfra getInfra(int id) {

		RedInfra resultList = null;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getInfrareds();
		}
		for (RedInfra infrared : list) {
			if (infrared.getId() == id) {
				resultList = infrared;
				break;
			}
		}

		return resultList;
	}

	public static int isExite(int infrad_key) {
		int result = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getInfrareds();
		}
		for (RedInfra infrared : list) {
			if (infrared.getInfraredKey() == infrad_key) {
				result = 1;
				break;
			}
		}

		return result;
	}

	public static void clearList() {
		list = MyApplication.getInstance().getWidgetDataBase().getInfrareds();
	}
}
